<?php

echo PHP_EOL . "<h3>输出</h3>" . PHP_EOL;
$name = "runoob";
$a = <<<EOF
            "abc"$name
            "123"
    EOF;
// 结束需要独立一行且前后不能空格
echo $a;

echo PHP_EOL . "<h3>变量</h3>" . PHP_EOL;
$x = 5;              //整形
$x = 10.365 + $x;    //浮点
$x = true;             //boolean
var_dump($x);
//数组
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);    #排序
echo count($cars);
echo "下标0：" . $cars[0];
//数组2
$a = array('a', 3 => 'b', 1 => 'c', 'd');
echo $a[4]; //输出 'd';
//Map
$age = array("Peter" => "35", "Ben" => "37", "Joe" => "43");


echo PHP_EOL . "<h3>常量/魔术常量</h3>" . PHP_EOL;
define("GREETING", "欢迎访问 Runoob.com", true);    //不区分大小写
echo greeting;  // （常量方法内部也可以使用）
#内部常量
echo '这是第 " ' . __LINE__ . ' " 行';
echo '该文件位于 " ' . __FILE__ . ' " ';
echo '该文件位于 " ' . __DIR__ . ' " ';
echo '命名空间为："', __NAMESPACE__, '"';


echo PHP_EOL . "<h3>字符串</h3>" . PHP_EOL;
$txt1 = "Hello world!";
$txt2 = "What a nice day!";
echo $txt1 . " " . $txt2;       //拼接
echo strlen("Hello world!");    //长度
echo strpos("Hello world!", "world");    //查找


echo PHP_EOL . "<h3>运算</h3>" . PHP_EOL;
#if & else
$t = date("H");
if ($t < "20") {
    echo "Have a good day!";
} else {
    echo "Have a good night!";
}
#switch
$favcolor = "red";
switch ($favcolor) {
    case "red":
        echo "你喜欢的颜色是红色!";
        break;
    case "blue":
        echo "你喜欢的颜色是蓝色!";
        break;
    default:
        echo "你喜欢的颜色不是 红, 蓝!";
}
#循环
$i = 1;
while ($i <= 5) {
    echo "The number is " . $i . "<br>";
    $i++;
}
$i = 1;
do {
    $i++;
    echo "The number is " . $i . "<br>";
} while ($i <= 5);
for ($i = 1; $i <= 5; $i++) {
    echo "数字为 " . $i . PHP_EOL;
}
foreach ($age as $value) {
    echo $value . PHP_EOL;
}
foreach ($age as $key => $value) {
    echo "key  为 " . $key . "，对应的 value 为 " . $value . PHP_EOL;
}


echo PHP_EOL . "<h3>函数</h3>" . PHP_EOL;
function add($x, $y)
{
    echo '类名为：' . __CLASS__ . "<br>";
    echo '函数名为：' . __METHOD__;
    echo '函数名为：' . __FUNCTION__;
    $total = $x + $y;
    return $total;
}

echo "1 + 16 = " . add(1, 16);

?>

    <a href="basis.php?subject=PHP&web=runoob.com">Test $GET</a>


<?php
echo PHP_EOL . "<h3>超级全局变量</h3>" . PHP_EOL;
$GLOBALS['z'] = 1 + 100;            //函数体内引用全局变量，必须在函数内部使用“global”关键字
echo $z;
echo $_SERVER['PHP_SELF'];
echo "Study " . $_GET['subject'] . " @ " . $_GET['web'];
?>